﻿using CPFrameWork.Global;
using CPFrameWork.UIInterface.Grid;
using CPFrameWork.Utility.DbOper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BusinessManage.ParentsGeneration
{
    public class ParentsGeneration
    {
        public CPWebApiBaseReturnEntity DeleteYW_OrderDetail(string filename,string filevalue, DbHelper dbHelper)
        {
            CPWebApiBaseReturnEntity re = new CPWebApiBaseReturnEntity();
            try
            {
                #region 获取订单信息
                StringBuilder orderBuilder = new StringBuilder();
                string ordersql = @"select * from DBO.YW_OrderInfo where "+filename+"=" + filevalue + " ";
                orderBuilder.Append(ordersql);
                DataTable datat = dbHelper.ExecuteDataSet(orderBuilder.ToString()).Tables[0];
                #endregion

                #region 删除订单详情
                StringBuilder detailBuilder = new StringBuilder();
                string detailsql = @"delete from DBO.YW_OrderDetail where OrderId='" + datat.Rows[0]["OrderCode"] + "' ";
                detailBuilder.Append(detailsql);
                dbHelper.ExecuteNonQuery(detailBuilder.ToString());
                #endregion

                re.Result = true;
                return re;
            }
            catch (Exception ex)
            {
                re.Result = false;
                re.ErrorMsg = ex.Message.ToString();
                return re;
            }
        }

        /// <summary>
        /// 新增主表/辅表数据
        /// </summary>
        /// <param name="ordersql"></param>
        /// <param name="grid"></param>
        /// <returns></returns>
        public bool InsertYW_OrderInfo(string ordersql, CPGrid grid)
        {
            bool result = false;
            try
            {
                DbHelper dbHelper = new DbHelper(grid.DbIns, CPAppContext.CurDbType());
                StringBuilder detailBuilder = new StringBuilder();
                detailBuilder.Append(ordersql);
                dbHelper.ExecuteNonQuery(detailBuilder.ToString());
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 通过部门名称返回部门编码
        /// </summary>
        /// <param name="deptname"></param>
        /// <returns></returns>
        public string GetDeptIDByName(string deptname)
        {
            StringBuilder orderBuilder = new StringBuilder();
            DbHelper dbHelper = new DbHelper("CPOrganIns", CPAppContext.CurDbType());
            string ordersql = @"select * from dbo.CP_Dep where  DepName='"+deptname+"'";
            orderBuilder.Append(ordersql);
            DataTable datat = dbHelper.ExecuteDataSet(orderBuilder.ToString()).Tables[0];
            if (datat.Rows.Count > 0) return datat.Rows[0]["DepId"].ToString();
            else return "";
        }


        /// <summary>
        /// 通过字典数据名称获取字典编码
        /// </summary>
        /// <param name="CodeTitle"></param>
        /// <returns></returns>
        public string GetCodeIdByCodeTitle(string CodeTitle,string CodeType)
        {
            StringBuilder orderBuilder = new StringBuilder();
            DbHelper dbHelper = new DbHelper("CPCommonIns", CPAppContext.CurDbType());
            string ordersql = @"select * from dbo.CP_Code where  CodeTitle='" + CodeTitle + "' and CodeType='" + CodeType + "'";
            orderBuilder.Append(ordersql);
            DataTable datat = dbHelper.ExecuteDataSet(orderBuilder.ToString()).Tables[0];
            if (datat.Rows.Count > 0) return datat.Rows[0]["CodeId"].ToString();
            else return "";
        }

    }
}
